package com.lm56.web.express;

import com.lm56.common.MyResponseBody;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Kelvin Yeung on 2016/11/12.
 */
@Controller
@RequestMapping(value = "/express")
public class ExpressController {


    Logger logger = LoggerFactory.getLogger(ExpressController.class);


    @Autowired
    ExpressService expressService;

    @RequestMapping(value="",method = RequestMethod.GET)
    public String express(Model model)
    {
        List<Express> expressList = expressService.getExpressList();
        model.addAttribute("expressList",expressList);
        return "search/express";
    }



    @RequestMapping(value = "info" ,method = RequestMethod.GET)
    @ResponseBody
    public MyResponseBody info(String com,String nu){

        if(StringUtils.isBlank(nu)){
            return new MyResponseBody(MyResponseBody.STATUS_ERROR,"暂无记录");
        }
        try {
            Map expInfos = expressService.getExpInfo(com, nu);
            return new MyResponseBody(MyResponseBody.STATUS_SUCESS,"获取快递信息成功",expInfos);

        }catch (Exception e){
            e.printStackTrace();
            return  new MyResponseBody(MyResponseBody.STATUS_ERROR,"暂无记录");
        }
    }

    @RequestMapping(value = "list",method = RequestMethod.GET)
    @ResponseBody
    public MyResponseBody expressList(){

        try{
            List<Express> expressList = expressService.getExpressList();
            return new MyResponseBody(MyResponseBody.STATUS_SUCESS,"获取快递信息成功",expressList);
        }catch (Exception e){
            return new MyResponseBody(MyResponseBody.STATUS_ERROR,"获取快递信息失败");
        }
    }


}
